﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;

namespace QLThuChi.OtherForms
{
    #region Using
    using QLThuChi.Models;
    using QLThuChi.Repositories;
    using QLThuChi.Helpers;
    #endregion

    /// <summary>
    /// tuansl added: insert new khenthuong progress
    /// </summary>
    public partial class FrmChonGiaiDoan : DevComponents.DotNetBar.Office2007Form
    {
        public EventHandler Handler { get; set; }
        // Hidden files are used to store ids 
        private DevComponents.DotNetBar.Controls.TextBoxX txtMaGiaiDoan;

        public FrmChonGiaiDoan()
        {
            InitializeComponent();
            InitHiddenFields();
        }

        /// <summary>
        /// Init hidden fields
        /// </summary>
        public void InitHiddenFields()
        {
            // Add a new textbox
            txtMaGiaiDoan = new DevComponents.DotNetBar.Controls.TextBoxX
            {
                Name = "txtMaGiaiDoan",
                Text = ""
            };
            txtMaGiaiDoan.Visible = false;
        }

        private void txtSoLuong_KeyPress(object sender, KeyPressEventArgs e)
        {
            // Only allow type number
            if (!char.IsNumber(e.KeyChar) && (Keys)e.KeyChar != Keys.Back)
            {
                e.Handled = true;
            }
        }

        private void btnChonGiaiDoan_Click(object sender, EventArgs e)
        {
            FrmQuanLyGiaiDoan frm = new FrmQuanLyGiaiDoan(true);
            frm.Handler += GetGiaiDoan;
            frm.ShowDialog();
        }

        public void GetGiaiDoan(object sender, EventArgs e)
        {
            var eventType = (MyEvent)e;
            string[] comp = eventType.Data.Split(new char[] { '#' });

            txtMaGiaiDoan.Text = comp[0];
            txtGiaiDoan.Text = comp[1];
        }

        /// <summary>
        /// Validate user input
        /// </summary>
        /// <param name="isUpdate"></param>
        /// <returns></returns>
        private bool ValidateInput(ref string errorText)
        {
            if (txtMaGiaiDoan.Text == "")
            {
                errorText = "Vui lòng chọn GiaiDoan";
                return false;
            }
            if (dtNgayBD.Value == DateTime.MinValue)
            {
                errorText = "Vui lòng nhập ngày bắt đầu";
                return false;
            }
            if (dtNgayKT.Value == DateTime.MinValue)
            {
                errorText = "Vui lòng nhập ngày kết thúc";
                return false;
            }
            return true;
        }

        private void btnCapNhat_Click(object sender, EventArgs e)
        {
            var errorText = "";
            if (!ValidateInput(ref errorText))
            {
                MessageBox.Show(errorText, "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            TransferDataInfo(this, new MyEvent(txtMaGiaiDoan.Text + "#" + dtNgayBD.Text + "#" + dtNgayKT.Text));
        }

        /// <summary>
        /// tuansl added: function is used to transfer data when event would be raised
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void TransferDataInfo(object sender, MyEvent e)
        {
            this.Close();
            this.Handler(this, e);
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            Close();
        }  
    }
}